package edu.columbia.at2337.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Alvin Ting at2337@columbia.edu
 *
 */
public class WordManager implements IWordManager {

	private List<String> mSecretDictionary = null;	
	private String mSecretWord = null;
	private Map<String, List<Integer>> mLetterMap = null;
	
	public WordManager() {
		mLetterMap = new HashMap<String, List<Integer>>();
		mSecretDictionary = new ArrayList<String>();
		
		mSecretDictionary.add("microwave");
		mSecretDictionary.add("applebees");
		mSecretDictionary.add("columbia");
		mSecretDictionary.add("zebra");
		mSecretDictionary.add("anchor");
	}

	public String generateNewSecretWord() {		
		
		if (!mSecretDictionary.isEmpty()) {
			mSecretWord = mSecretDictionary.remove(0);			
		} else {
			mSecretWord = "hidden";
		}
		
		// Reset map for new secret word
		mLetterMap.clear();
		
		List<Integer> currentList = null;
		String alpha = "";
		int index = 0;
		for (char letter : mSecretWord.toCharArray()) {	
			alpha = new Character(letter).toString();
			
			if (mLetterMap.containsKey(alpha)) {
				currentList = mLetterMap.get(alpha); 
				currentList.add(index);
			} else {
				currentList = new ArrayList<Integer>();
				currentList.add(index);
				mLetterMap.put(alpha, currentList);				
			}
			//System.out.println("key = " + letter + " index = " + index);
			
			index++;			
		}
		
		System.out.println("The secret word is: " + mSecretWord);
		
		return mSecretWord;
	}

	public List<Integer> getOccurenceOf(String letter) {		
		return mLetterMap.get(letter);
	}
	
}
